#include <iostream>

using namespace std;

template <class T>
struct Node
{
    T data;
    Node* front;
};

template <class T>
class STACK
{
public:
    STACK() 
    {
    top=(Node<T>*)new Node<T>;
    top->front=NULL;
    top->data=(T) 0;
    };
    ~STACK()
    {
        while(top->front!=NULL)
        {
            Node<T>* temp=top->front;
            free(top);
            top=temp;
        }
    };
    void push(T element);
    void pop();
    void display();
    int size();
    T topelement();
private:
    Node<T>* top;
    int number;
};


template <class T>
void STACK<T>::push(T element)
{
    Node<T>* temp=(Node<T>*)new Node<T>;
    temp->front=top;
    temp->data=element;
    top=temp;
};

template <class T>
void STACK<T>::pop()
{
    Node<T>* temp=top;
    top=top->front;
    free(temp);
};

template <class T>
void STACK<T>::display()
{
    Node<T>* temp=top;
    while(temp->front!=NULL)
    {
        cout<<temp->data<<"  ";
        temp=temp->front;
    }
    cout<<endl;
};

template <class T>
int STACK<T>::size()
{
    number=0;
    Node<T>* temp=top;
    while(temp->front!=NULL)
    {
        number++;
        temp=temp->front;
    }
    return number;
}

template <class T>
T STACK<T>::topelement()
{
    return top->data;
};
    
int main()
{
    STACK<int> a;
    for(int i=1;i<=5;i++)
    {
        a.push(i*3);
    }
    a.display();
    cout<<a.size()<<endl;
    cout<<a.topelement()<<endl;
    a.pop();
    a.display();
    cout<<a.size()<<endl;
    cout<<a.topelement()<<endl;

    system("pause");
    return 0;
}

